// 2013-7-28下午01:41:23  net.betou.trade.admin.action.AdministratorsRoleAct.java

package net.betou.trade.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.util.TradeUtils;
import net.betou.trade.action.abs.ActionSupport;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Role;
import net.betou.trade.main.manager.role.SystemRoleMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.ResponseUtil;
import net.betou.trade.web.WebErrors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 管理员角色Action
 * @author 叶装装
 */
@Controller
public class AdminsRoleAct extends ActionSupport < Role >{
	private static final Logger log = LoggerFactory.getLogger( AdminsRoleAct.class );
	
	private static final String DIRECTORY = "role/system";
	
	@RequestMapping( "/systemrole/v_list.jspx" )
	public String v_list( String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		 List< Role > list = manager.list( admin, searchProperty, searchValue, orderProperty, orderDirection );
		 this.paramToModel( model, null, list, null, searchProperty, searchValue, orderProperty, orderDirection, null );
		 
		 return getListPage( );
		
	}
	
	@RequestMapping( "/systemrole/v_add.jspx" )
	public String v_add( HttpServletRequest request, ModelMap model ){
		return getAddPage( );
	}
	
	@RequestMapping( "/systemrole/o_save.jspx" )
	public String o_save( Role bean, String [ ] perms, ModelMap model, HttpServletRequest request ){
		
		WebErrors errors = validateSave( bean, perms );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		bean = manager.save( bean, this.splitPerms( perms ) );
		
		logMng.operating( 0, request, Messages.LOG_SYSTEM_ROLE, "id=" + bean.getId( ) + ",name=" + bean.getName( ));
		
		return "redirect:v_list.jspx";
	}
	
	@RequestMapping( "/systemrole/v_edit.jspx" )
	public String v_edit( Integer id,  String type, HttpServletRequest request, ModelMap model ) {
		WebErrors errors = validateUpdate( id );
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		Role bean = manager.findRole( id );
		
		model.addAttribute( "bean", bean );
		
		return getUpdatePage( );
	}
	
	/**
	 * @version 1.1
	 * @return
	 */
	@RequestMapping( "/systemrole/o_update.jspx" )
	public String o_update(  Integer id, String [ ] perms,  Boolean allowInit, String name, Integer level, String description, HttpServletRequest request, ModelMap model ) {
		
		WebErrors errors = validateUpdate( id );

		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		// 先是查找出这个角色
		Role bean = manager.findRole( id );
		
//		// 如果是系统默认, 直接返回去，不进行任何操作
//		if( bean.getRoleDefault( ) ){
//			return "redirect:v_list.jspx";
//		}
		
		bean = manager.update( allowInit, description, level, name, bean, this.splitPerms( perms )  );
		
		logMng.operating( 1, request, Messages.LOG_SYSTEM_ROLE, "id=" + bean.getId( ) + ",name=" + bean.getName( ));
		
		return "redirect:v_list.jspx";
		
	}
	
	@RequestMapping( "/systemrole/o_delete.jspx" )
	public void delete( Integer [ ] ids, HttpServletRequest request, HttpServletResponse response ) {
		ResponseUtil util = validateDelete( ids, response );
		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}

		Role [ ] beans = manager.deleteByIds( ids );
		
		for ( Role bean : beans ){
			log.info( "delete AdminsRole id={}", bean.getId( ) );
			logMng.operating( 2, request, Messages.LOG_SYSTEM_ROLE  , "id=" + bean.getId( ) + ",name=" + bean.getName( ));
		}

		util.addSuccessResponse( Messages.getDeleteSuccessMessage( beans.length ) );

		util.renderJson( );
		return;
	}
	
	
	private ResponseUtil validateDelete( Integer [ ] ids, HttpServletResponse response ) {
		ResponseUtil util = ResponseUtil.create( response );

		if( ids == null || ids.length == 0 ){
			util.addErrorResponse( Messages.DATA_NOT_SELECT );
			return util;
		}

		for ( Integer id : ids ){
			Role role = manager.findRole( id );
			if( role == null ){
				util.addErrorResponse( Messages.getNotExistMessage( id ) );
				break;
			}else if( role.getRoleDefault( ) ){
				util.addErrorResponse( Messages.ROLE_MESSAGE_DEFAULT_NOT_DELETE );
				break;
			}
		}
		return util;
	}
	
	private WebErrors validateUpdate( Integer id ){
		
		WebErrors errors = WebErrors.create( );

		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}

		Role bean = manager.findRole( id );
		if( bean == null ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		return errors;
	}
	
	private WebErrors validateSave( Role bean, String [ ] perms ){
		
		WebErrors errors = WebErrors.create( );
		String name = bean.getName( );

		if( errors.ifOutOfLength( name, "角色名称", 1, 100 ) ){
			return errors;
		}
		
		if( perms == null || perms.length == 0 ){
			errors.addError(  Messages.ROLE_MESSAGE_NOT_CHOOSE_PERMISSION );
		}
		
		return errors;
	}
	
	@Autowired
	private SystemRoleMng manager;
	
	@Override
	protected String getDirectory( ) {
		return DIRECTORY;
	}

}
